#ifndef __ANI_CONFIG_H__
#define __ANI_CONFIG_H__

#include "../tinyxml/tinyxml.h"
#include "cocos2d.h"
#include <string>
#include <map>
#include <vector>
namespace fishingjoy
{
	namespace config
	{

		struct SAniData
		{
			std::string Name;
			std::string File;
			int FrameNum;
			float Delay;
			cocos2d::CCArray* Frames;
			SAniData()
				:Frames(NULL)
			{

			}
		};

		class CAniObjectData
		{
			std::vector<SAniData> mAnis;
		public:
			const SAniData* getAni(const char* name)const
			{
				std::vector<SAniData>::const_iterator it = mAnis.begin();
				for(;it!=mAnis.end();++it){
					if(it->Name == name){
						return &(*it);
					}
				}
				return NULL;
			}

			const char* getDefaultAni()const
			{
				if(mAnis.size()){
					return mAnis[0].Name.c_str();
				}
				return NULL;
			}

			SAniData& addNewAni()
			{
				mAnis.push_back(SAniData());
				return mAnis[mAnis.size()-1];
			}
		};

		class CAniConfig
		{	
			static std::map<int,CAniObjectData> m_mapAniData;
			static void load()
			{
				if(m_mapAniData.size() ==0){
					TiXmlDocument doc;
					std::string pathKey = "config/animations.xml"; 
					pathKey = cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(pathKey.c_str());		
					//const char* pFile = cocos2d::CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("config/animations.xml");
					if(doc.LoadFile(pathKey.c_str())){
						TiXmlElement* root = doc.RootElement();
						TiXmlElement* pObject = root->FirstChildElement();
						while(pObject){
							CAniObjectData objData;
							int tid = atoi(pObject->Attribute("template_id"));
							
							TiXmlElement* pAniNode = pObject->FirstChildElement();
							while(pAniNode){
								SAniData& aniData = objData.addNewAni();
								aniData.Name = pAniNode->Attribute("name");
								aniData.File = pAniNode->Attribute("file");
								aniData.FrameNum = atoi(pAniNode->Attribute("frames"));
								aniData.Delay = atof(pAniNode->Attribute("delay"));
								pAniNode = pAniNode->NextSiblingElement();

		
								
								aniData.Frames = cocos2d::CCArray::create();
								aniData.Frames->retain();
								cocos2d::CCTexture2D* pTexture = cocos2d::CCTextureCache::sharedTextureCache()->addImage(aniData.File.c_str());
								int with = pTexture->getPixelsWide();
								int height = pTexture->getPixelsHigh()/aniData.FrameNum;
								for(int i = 0; i < aniData.FrameNum; ++i){
									cocos2d::CCSpriteFrame* pFrame = cocos2d::CCSpriteFrame::createWithTexture(pTexture,cocos2d::CCRectMake(0,i*height,with,height));
									aniData.Frames->addObject(pFrame);
								}
							}

							m_mapAniData[tid] = objData;
							pObject = pObject->NextSiblingElement();
						}
					}
				}
			}
		public:
			static const CAniObjectData* getData(int tid)
			{
				load();
				std::map<int,CAniObjectData>::iterator it = m_mapAniData.find(tid);
				if(it!=m_mapAniData.end()){
					return &it->second;
				}
				printf("can't find ani object data %d",tid);
				return NULL;
			}
		};
	};
}
#endif